<template>


  <div>
    <button type="info" @click="exportToExcel">导出所有操作日志为Excel</button>
  </div>

</template>

<script>
export default {
  name: "Home",
  data(){
    return{

      tableData: []
    }
  },

  methods: {
    exportToExcel(){
      this.axios.get("admin/operationLog/list").then(res => {
        this.tableData = res.data.operationLogs;
        const customHeaders = ['物业操作人员', '住户操作人员', '登录IP地址', '操作'];
        const customData = this.tableData.map(item => ({
          '物业操作人员': item.admin?.adminName ?? '无',
          '住户操作人员': item.user?.userName ?? '无',
          '登录IP地址': item.ip ?? '无',
          '操作': item.info ?? '无'
        }));

// 2. 生成工作表
        const worksheet = XLSX.utils.json_to_sheet(customData);

// 3. 覆盖表头（确保名称和顺序一致）
        XLSX.utils.sheet_add_aoa(worksheet, [customHeaders], { origin: 'A1' });

// 4. 删除多余列（如果有）
        const maxCol = XLSX.utils.decode_range(worksheet['!ref']).e.c;
        if (maxCol > customHeaders.length - 1) {
          for (let C = customHeaders.length; C <= maxCol; C++) {
            XLSX.utils.sheet_delete_cols(worksheet, C);
          }
        }


// 6. 保存文件（后续代码不变）
        const workbook = XLSX.utils.book_new();
        XLSX.utils.book_append_sheet(workbook, worksheet, '用户信息');
        const excelBuffer = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
        const blob = new Blob([excelBuffer], { type: 'application/octet-stream' });
        saveAs(blob, '操作日志.xlsx');
      });
    }
  }
}
</script>

<style>


</style>